package com.yellowpage.nsp.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.nsp.dao.NspFetchLogDao;
import com.yellowpage.nsp.dao.NspLoggerDao;
import com.yellowpage.nsp.dao.NspTelLogDao;
import com.yellowpage.nsp.dao.RemoteNspDAO;
import com.yellowpage.nsp.dao.RemoteNspUserDAO;
import com.yellowpage.nsp.data.NspFetchLog;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class NspFetchLogServiceImpl extends BaseServiceImpl implements NspFetchLogService {
	  private NspFetchLogDao nspFetchLogDao;
	  private NspLoggerDao nspLoggerDao;
	  private NspTelLogDao nspTelLogDao;
	
	  public void setNspFetchLogDao(NspFetchLogDao nspFetchLogDao) {
		  this.nspFetchLogDao = nspFetchLogDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public NspFetchLog getNspFetchLog(Long id){
	      return nspFetchLogDao.get(id);
	  }
	  
    public List<NspFetchLog> getAllNspFetchLog(){
        return nspFetchLogDao.getAll();
    }
    
    public List<Map> queryNspFetchLogByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.FETCH_LOG_ID , a.SERVER_ID , a.LOG_FILE_NAME , a.FETCH_TYPE , a.FETCH_TABLE , a.NOTE , a.START_LOG_ID , a.END_LOG_ID , a.END_TIME , a.START_TIME , a.STATUS    FROM NSP_FETCH_LOG a WHERE 1=1 ")
				    			.addCondition(" and FETCH_LOG_ID = :fetchLogId ")
				    			.addCondition(" and SERVER_ID = :serverId ")
				    			.addCondition(" and LOG_FILE_NAME = :logFileName ")
							    			.addCondition(" and FETCH_TYPE = :fetchType ")
							    			.addCondition(" and FETCH_TABLE = :fetchTable ")
							    			.addCondition(" and NOTE = :note ")
							    			.addCondition(" and START_LOG_ID = :startLogId ")
							    			.addCondition(" and END_LOG_ID = :endLogId ")
							    		    .addCondition(" and date_format(END_TIME,'%Y-%m-%d') =  :endTime  ")
		    				    		    .addCondition(" and date_format(START_TIME,'%Y-%m-%d') =  :startTime  ")
		    				    			.addCondition(" and STATUS = :status ")
							    ; 		    
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    
    public void fetchNspLogData(NspFetchLog nspFetchLog) {		
        
    	int maxLoggerId = 0;
    	int maxTellogId = 0;
    	
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
		"SELECT max(ID) as MAXID FROM NSP_LOGGER a WHERE 1=1 ");
		
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(), new HashMap());
		
		maxLoggerId = ((Integer)resultList.get(0).get("MAXID")).intValue();
							    			
		sqlAssistant = baseDao.newQueryAssistantSQL(
		"SELECT max(ID) as MAXID  FROM NSP_TEL_LOG  WHERE 1=1 "); 
		
		resultList = sqlAssistant.query(baseDao.getConnection(), new HashMap());
		
		maxTellogId = ((Integer)resultList.get(0).get("MAXID")).intValue();
		
        RemoteNspDAO remoteNspDAO = new RemoteNspDAO();
		
		List nspLoggerList = remoteNspDAO.getNspLogger(String.valueOf(maxLoggerId));
		List nspTellogList = remoteNspDAO.getNspTellog(String.valueOf(maxTellogId));
		
		nspLoggerDao.save(nspLoggerList) ;
		nspTelLogDao.save(nspTellogList);    
		
		
		nspFetchLog.setEndTime(new Date());
		nspFetchLog.setStatus("COMPLETE");
		nspFetchLogDao.update(nspFetchLog);
		
	}
    
    public List<Map> getNSPSYSUpdateTime(String table){
    	
    	QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
		"SELECT * FROM NSP_FETCH_LOG WHERE FETCH_TABLE='"+table+"'");
		
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(), new HashMap());
    	return resultList;
    }
    
    public List<Map> getNSPUserAccount(String updateTime){
    	RemoteNspUserDAO remoteNspUserDAO = new RemoteNspUserDAO();
    			
    	return remoteNspUserDAO.getNspUserAccount(updateTime);
    	
    }
    
    public List<Map> getNSPUserPayAccount(String updateTime){
    	RemoteNspUserDAO remoteNspUserDAO = new RemoteNspUserDAO();
		
    	return remoteNspUserDAO.getNspUserPayAccount(updateTime);
    }
    public List<Map> getNspUserPerson(String updateTime){
    	RemoteNspUserDAO remoteNspUserDAO = new RemoteNspUserDAO();
		
    	return remoteNspUserDAO.getNspUserPerson(updateTime);
    }   
    
    
    
    public void saveNspFetchLog(NspFetchLog nspFetchLog){
        nspFetchLogDao.save(nspFetchLog);
    }
    
	  public void updateNspFetchLog(NspFetchLog nspFetchLog){
	      nspFetchLogDao.update(nspFetchLog);
	  }
	  
	  public void delete(List deleteList){
	      nspFetchLogDao.remove(deleteList);
	  }


	public NspLoggerDao getNspLoggerDao() {
		return nspLoggerDao;
	}


	public void setNspLoggerDao(NspLoggerDao nspLoggerDao) {
		this.nspLoggerDao = nspLoggerDao;
	}


	public NspTelLogDao getNspTelLogDao() {
		return nspTelLogDao;
	}


	public void setNspTelLogDao(NspTelLogDao nspTelLogDao) {
		this.nspTelLogDao = nspTelLogDao;
	}




	public NspFetchLogDao getNspFetchLogDao() {
		return nspFetchLogDao;
	}  
	  
	 
	
}	